package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping("login")
    public String login(String userName, String pwd, HttpServletRequest request){
        UserBean user = service.login(userName, pwd);
        if(user == null){
            return "no";
        }
        request.getSession().setAttribute("loginUser", user);
        return "ok";
    }

    @RequestMapping("findByItem")
    public PageInfo<UserBean> findByItem(int pageNO, String userName){
        return service.findByItem(pageNO, userName);
    }

    @RequestMapping("add")
    public String addUser(UserBean user, int[] roleArray ){
        service.addUser(user, roleArray);
        return "ok";
    }

    @RequestMapping("del")
    public String delUser(int id){
        service.delUser(id);
       return "ok";
    }

    @RequestMapping("findById")
    public UserBean findById(int id){
        return service.findById(id);
    }

    @RequestMapping("update")
    public String updateUserGrade(int id,int[] roleArray){
        service.updateUserGrade(id, roleArray);
        return "ok";
    }

    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest request){
       UserBean user = (UserBean) request.getSession().getAttribute("loginUser");
        return user;
    }
}
